package com.enjoyor.soa.traffic.rest.efoms.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.efoms.TemplateDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.rest.efoms.dubbo.IAATemplateConsumeService;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

@Auth
@RestController
@RequestMapping({ "/api/template" })
@Api("TemplateController相关api")
@CrossOrigin
public class TemplateExampleController {

    @Autowired
    private IAATemplateConsumeService templateConsumeService;
    
   /* @Autowired
    @GetMapping(value = { "/select" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("根据id查询操作记录")
    @ApiImplicitParam(name = "id", value = "编号", defaultValue = "", paramType = "query", required = true, dataType = "String")
    public ResultPojo select(
    		@RequestParam(defaultValue = "",value="id",required=true) String id, 
    		HttpServletRequest request) {
        return templateConsumeService.select(id);
    }*/
    /**
	 * 获取模板列表信息
	 * @param id
	 * @param type
	 * @param content
	 * @return
	 */
	@GetMapping(value = { "selectList" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiResponse(response=TemplateDto.class, code = 0, message = "")
    @ApiOperation("模板列表信息查询")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "编号", paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "type", value = "类型", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "content", value = "内容", paramType = "query", dataType = "String") })
	public ResultPojo getTemplateInfoList(
			@RequestParam(defaultValue = "",value="id",required=false) String id,
			@RequestParam(defaultValue = "",value="type",required=false) String type,
			@RequestParam(defaultValue = "",value="content",required=false) String content,
			/*@RequestParam(value="startTime",required=false) Date startTime,*/
			HttpServletRequest request) {
		return templateConsumeService.selectList(id, type, content);
	}
    @SuppressWarnings({ "rawtypes" })
    @GetMapping(value = { "/selectPage" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "timeStart", value = "开始时间", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "timeEnd", value = "结束时间", paramType = "query", dataType = "String")})
    public ResultPojo selectPage(
    		@RequestParam(defaultValue = "",value="timeStart",required=false) String timeStart,
			@RequestParam(defaultValue = "",value="timeEnd",required=false) String timeEnd,
    		Page page, HttpServletRequest request) {
        return templateConsumeService.selectPage(page, timeStart, timeEnd);
    }

    @PostMapping(value = { "/create" }, consumes = { "application/json; charset=UTF-8" }, produces = {
            "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("创建数据")
    public ResultPojo create(@RequestBody TemplateDto templateDto, HttpServletRequest request) {
        return templateConsumeService.insert(templateDto,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    @PutMapping(value = { "/update" }, consumes = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("更新数据")
    public ResultPojo update(@RequestBody TemplateDto templateDto, HttpServletRequest request) {
        return templateConsumeService.edit(templateDto,
                ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

    @DeleteMapping(value = { "/delete" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("删除数据")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true, dataType = "String")
    public ResultPojo delete(@PathVariable("id") String id, HttpServletRequest request) {
        return templateConsumeService.delete(id, ((UserDto) request.getAttribute("currentUser")).getPersonName());
    }

}
